package com.qkl.xiatang.app.utils;

import com.alibaba.fastjson.JSON;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.UUID;

public class DahuaUtils {

    private static final String WRAP = Constants.ENTER_LINE;

    public static long timestamp(){
        return System.currentTimeMillis();
    }

    public static String nonce(){
        return AppUtils.genIdStr();
    }

    public static String sha512Hex(String data){
        return DigestUtils.sha512Hex(StringUtils.deleteWhitespace(data));
    }

    public static String stringToSign(String httpMethod, Object body, Map<String,String> signHeaders){
        String contentSha512 = sha512Hex( null == body ? "": JSON.toJSONString(body));
        return stringToSign(httpMethod,contentSha512,signHeaders);
    }

    public static String stringToSign(String httpMethod, String contentSha512, Map<String,String> signHeaders){
        String contentSha = StringUtils.trimToEmpty(contentSha512);
        return httpMethod + WRAP + contentSha + WRAP + headersToSign(signHeaders);
    }

    public static String sign(String accessKey,long timestamp ,String nonce ,String stringToSign,String secretAccessKey){
        return sign(accessKey,"",timestamp,nonce,stringToSign,secretAccessKey);
    }

    public static String sign(String accessKey, String accessToken,long timestamp ,String nonce ,String stringToSign,String secretAccessKey){
        String appAccessToken = StringUtils.trimToEmpty(accessToken);
        String str = accessKey + appAccessToken + timestamp + nonce + stringToSign;
        return HmacUtils.encryptSHA512(str,secretAccessKey).toUpperCase();
    }

    public static Map<String,String> toHeaders(String accessKey,long timestamp,String nonce ,String sign,String productID ){
        return toHeaders(accessKey,null,timestamp,nonce,sign,productID);
    }

    public static Map<String,String> toHeaders(String accessKey,String accessToken,long timestamp,String nonce ,String sign,String productID ){
        String appAccessToken = StringUtils.trimToEmpty(accessToken);
        Map<String,String> headers = new HashMap<>();
        headers.put("Content-Type","application/json");
        headers.put("Accept-Language","en-US");
        headers.put("Version","V1");
        headers.put("AccessKey",accessKey);
        headers.put("Timestamp",String.valueOf(timestamp));
        headers.put("Nonce",nonce);
        headers.put("Sign",sign);
        headers.put("ProductID",productID);
        headers.put("X-TraceId-Header", UUID.randomUUID().toString().replaceAll("-",""));
        if(StringUtils.isNotEmpty(appAccessToken)){
            headers.put("AppAccessToken",appAccessToken);
        }
        return headers;
    }

    private static String headersToSign(Map<String,String> signHeaders){
        if(ObjectUtils.isEmpty(signHeaders)){
            return WRAP;
        }
        Set<Map.Entry<String, String>> set = signHeaders.entrySet();
        String str = "";
        for(Map.Entry<String, String> entry : set){
            str = str + entry.getKey() + ":" + entry.getValue() + WRAP;
        }
        return str;
    }

    public static void main(String[] args) {
        System.out.println(sha512Hex(""));
        System.out.println(toHeaders("sfsdf","appptoekn",System.currentTimeMillis(),"sdfsdfds","sdfksdkf","1235456"));
        //System.out.println(DigestUtils.);
    }
}
